/**
 * 数据和分页初始化
 */
admin.controller('InitController', [
    '$scope',
    '$rootScope',
    '$http',
    'localStorageService',
    'util',
    function ($scope,
              $rootScope,
              $http,
              localStorageService,
              util) {
        /**
         * 取数据
         */
        var tables = [
          'usr'      , 'dept',  'major'    , 'classes',
          'role'     , 'term',  'att_item' , 'att_type',
          'att_form' ,
        ];
        $http
          .get( '/api/multi?resources='+tables.join() )
          .success(function (datas) {
            console.log(datas);
            $scope.map = {};
            angular.forEach(tables, function (tableName, i) {
                var item = datas[i];
                $scope[tableName] = item;
                $scope.map[tableName] = util.array2map(item);

                localStorageService.set(tableName,item);
            });
          })
        /**
        * 分页设置
        */
        $scope.itemsPerPage = 5; // 每页条目数
        // $scope.bigTotalItems = $scope.usr.length; // 总条目数
        $scope.bigCurrentPage = 1; // 当前页码
        /**
         * 因为查询过来的部门是对应的id号，所以在做筛选时候要做特殊处理
         * @param origin 各个筛选项的值，此处为部门id
         * @param input 筛选输入值
         * @returns {boolean} 如果为true表示匹配成功可以显示，否则匹配失败不予显示
         */
        $scope.deptFilter = function (origin, input) {
            var reg = new RegExp(input, 'g');
            var deptName = $scope.map.dept[origin].dept_name;
            return reg.test(deptName);
        };
        $scope.idFilter = function (origin, input) {
            var reg  = new RegExp(input, 'g');
            var field= 'dept';
            var name = $scope['map'][field][origin][field+'_name'];
            return reg.test(name) && (!!origin) ;
        };
        $scope.AttTypeFilter = function (origin, input) {
            var reg  = new RegExp(input, 'g');
            var field= 'att_type';
            var name = $scope['map'][field][origin][field+'_name'];
            return reg.test(name) && (!!origin) ;
        };
    }
]);


var stack_bottomright = {"dir1": "up", "dir2": "left", "firstpos1": 25, "firstpos2": 25};
function show_stack(type) {
  // PNotify.prototype.options.styling = "fontawesome";
  PNotify.prototype.options.styling = "bootstrap3";
  var opts = {
      title: "默认",
      text: "默认信息",
      addclass: "stack-bottomright",
      stack: stack_bottomright
  };
  var loadingOpts = {
    text: "操作执行中",
    type: 'info',
    icon: 'fa fa-spinner fa-spin',
    hide: false,
    buttons: {
        closer: false,
        sticker: false
    },
    opacity: .75,
    shadow: false,
    width: "170px",
    addclass: "stack-bottomright",
    stack: stack_bottomright
  }
  var successRemove = {
    title    : '删除成功',
    text     : '删除成功',
    type     : 'success' ,
    hide     : 'true'    ,
    icon     : 'fa fa-check',
    shadow   : true,
    width    : PNotify.prototype.options.width,
    opacity  : 1,
    stack    : stack_bottomright,
    addclass : "stack-bottomright",
    buttons  : {
      closer : true,
      sticker: true
    },
  };

  var successAdd = {
    title    : '添加成功',
    text     : '添加成功',
    type     : 'success' ,
    hide     : 'true'    ,
    icon     : 'fa fa-check',
    shadow   : true,
    width    : PNotify.prototype.options.width,
    opacity  : 1,
    stack    : stack_bottomright,
    addclass : "stack-bottomright",
    buttons  : {
      closer : true,
      sticker: true
    },
  };

  var successModify = {
    title    : '修改成功',
    text     : '修改成功',
    type     : 'success' ,
    hide     : 'true'    ,
    icon     : 'fa fa-check',
    shadow   : true,
    width    : PNotify.prototype.options.width,
    opacity  : 1,
    stack    : stack_bottomright,
    addclass : "stack-bottomright",
    buttons  : {
      closer : true,
      sticker: true
    },
  };

  PNotify.prototype.myOpts = {
    'success-remove' : successRemove ,
    'success-add'    : successAdd    ,
    'success-modify' : successModify ,
    'loading'        : loadingOpts ,
  }
  switch (type) {
    case 'error':
      opts.title = "错误提示";
      opts.text = "一些错误提示";
      opts.type = "error";
      break;
    case 'info':
      opts.title = "提示信息";
      opts.text = "一些提示";
      opts.type = "info";
      break;
    case 'success':
      opts.title = "设置成功";
      opts.text =  "该成员已成功退役，学长（学姐）走好";
      opts.type = "success";
      break;
    case 'success2':
      opts.title = "修改成功";
      opts.text =  "修改成功";
      opts.type = "success";
      break;
    case 'success-modify':
      opts = successModify;
      break;
    case 'success-add':
      opts = successAdd;
      break;
    case 'success-remove':
      opts = successRemove;
      break;
    case 'loading':
      opts = loadingOpts;
      break;
  }
  return new PNotify(opts);
}